package com.qb.controller;

import com.github.pagehelper.PageInfo;
import com.qb.entity.BlogWithTypeAndUname;
import com.qb.entity.DialogResult;
import com.qb.service.BlogService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;

import java.util.Map;

@Controller
@RequestMapping("blog")
public class BlogController {

    @Autowired
    private BlogService blogService;

    //全查
    @RequestMapping("findAll")
    @ResponseBody
    public PageInfo<BlogWithTypeAndUname> findAll(
            @RequestParam(name = "currentPage", defaultValue = "1") Integer currentPage,
            @RequestParam(name = "pageSize", defaultValue = "5") Integer pageSize
    ) {
        return blogService.findAll(currentPage, pageSize);
    }

    //删除按钮
    @RequestMapping("deleteInfo")
    @ResponseBody
    public DialogResult deleteInfo(Integer bid) {
        DialogResult result = new DialogResult();
        boolean flag = blogService.deleteInfo(bid);
        if (flag == true) {
            result.setMsg("删除成功！");
        } else {
            result.setError("删除失败！");
        }
        return result;
    }

    //详情按钮
    @RequestMapping("findBlogById")
    @ResponseBody
    public Map<String, Object> findBlogById(Integer bid) {
        DialogResult result = new DialogResult();
        Map<String, Object> map = blogService.findBlogById(bid);
        return map;
    }
}
